package com.xiaoxu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.xiaoxu.spzx.manager.mapper.OrderInfoMapper;
import com.xiaoxu.spzx.manager.mapper.OrderStatisticMapper;
import com.xiaoxu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class OrderStatisticsTask {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticMapper orderStatisticMapper;

    //统计 每天两点 的数据 并将数据添加到数据库中
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {

        //1.先要获取到前一天
        String createDate = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");

        //根据前一天数据 查询数据库
       OrderStatistics orderStatistics =  orderInfoMapper.selectOrderInfoByDate(createDate);


        //查出来的数据添加到数据库中
        if(orderStatistics != null) {
            orderStatisticMapper.insert(orderStatistics) ;
        }



    }
}
